﻿Imports Data

Public Class IngresoDeuda

    Private Property accion As Integer
    Private Property deuda As Data.Deudas


    Public Enum Acciones
        Ingresar = 1
        Modificar = 2
    End Enum

    Private Sub btnIngresar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnIngresar.Click
        Try
            If Me.accion = Acciones.Modificar Then
                Me.Modificar()
                Me.Close()
            Else
                Dim deuda As New Data.Deudas
                deuda.nombre = txtNombre.Text
                deuda.descripcion = txtDescripcion.Text
                deuda.fecha_pago = dtpFechaPago.Value
                deuda.total = txtTotal.Text
                deuda.id_estado = 2
                If Me.Ingresar(deuda) Then
                    Me.Close()
                End If
            End If
        Catch ex As Exception

        End Try
    End Sub

    Private Function Modificar() As Boolean
        Try
            Using db As New SerigraphicaDBDataContext
                Dim m_deuda As Data.Deudas = db.Deudas.Where(Function(p) p.id = Me.deuda.id).Select(Function(p) p).FirstOrDefault
                m_deuda.nombre = txtNombre.Text
                m_deuda.descripcion = txtDescripcion.Text
                m_deuda.fecha_pago = dtpFechaPago.Value
                m_deuda.total = txtTotal.Text
                m_deuda.id_estado = 2
                db.SubmitChanges()
            End Using
            Return True
        Catch ex As Exception
            Return False
        End Try
    End Function







    Private Function Ingresar(ByVal deudas As Data.Deudas) As Boolean
        Try
            Using db As New SerigraphicaDBDataContext
                db.Deudas.InsertOnSubmit(deudas)
                db.SubmitChanges()
            End Using
            Return True
        Catch ex As Exception
            Return False
        End Try
    End Function

    Private Function LimpiarInterfaz() As Boolean
        Try
            Me.txtNombre.Text = String.Empty
            Me.txtDescripcion.Text = String.Empty
            Me.txtTotal.Text = String.Empty
            Me.dtpFechaPago.Value = Date.Now
            Return True
        Catch ex As Exception
            Return False
        End Try
    End Function

    Private Sub btnCancelar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancelar.Click
        Me.Close()
    End Sub

    Public Sub New()
        InitializeComponent()

    End Sub

    Public Sub New(ByVal deuda As Data.Deudas, ByVal accion As Integer)
        InitializeComponent()
        Me.deuda = deuda
        Me.accion = accion
    End Sub



    Private Sub IngresoDeuda_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        If Me.accion = Acciones.Modificar Then
            Me.txtNombre.Text = deuda.nombre
            Me.txtDescripcion.Text = deuda.descripcion
            Me.txtTotal.Text = deuda.total
            Me.dtpFechaPago.Value = deuda.fecha_pago
            Me.btnIngresar.Text = "Modificar"

        End If
    End Sub
End Class